﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Heima.OA.BLL
{
    public partial class DepartmentService : BaseService<Department>, IDepartmentService
    {
        /// <summary>
        /// 获取部门编号
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public string GetDeptCode(int parentId)
        {
            string res = string.Empty;
            Department department = DbSession.DepartmentDal.GetModel(parentId);
            if(department != null)
            {
                var childList = DbSession.DepartmentDal.GetEntities(u => u.DelFlag==0 && u.ParentId== parentId).OrderBy(u => u.Sort).ToList();
                if (childList.Count > 0)
                {
                    string temp = childList[childList.Count - 1].DeptCode;
                    temp = temp.Substring(temp.Length - 2);
                    res = department.DeptCode + (DataConvert.GetInt(temp) + 1).ToString().PadLeft(2, '0');
                }
                else
                {
                    res = department.DeptCode + "01";
                }
            }
            else
            {
                var childList = DbSession.DepartmentDal.GetEntities(u => u.DelFlag == 0 && u.DeptCode.Length==2).OrderBy(u => u.Sort).ToList();
                if (childList.Count > 0)
                {
                    string temp = childList[childList.Count - 1].DeptCode;
                    res = (DataConvert.GetInt(temp) + 1).ToString().PadLeft(2, '0');
                }
                else
                {
                    res = "00";
                }
            }
            return res;
        }
        public List<DepartmentView> GetDepartmentView()
        {
            List<Department> deptTopList = DbSession.DepartmentDal.GetEntities(u => u.DelFlag == 0 && u.DeptCode.Length==2 ).OrderBy(u=>u.Sort).ToList();
            List<DepartmentView> deptViewList = new List<DepartmentView>();
            foreach(var item in deptTopList)
            {
                DepartmentView tree = new DepartmentView
                {
                    Id = item.Id,
                    DeptName = item.DeptName,
                    DeptCode = item.DeptCode,
                    ParentId = item.ParentId,
                    IsLeaf = item.IsLeaf,
                    IsCheck = item.IsCheck,
                    Sort = item.Sort,
                    Remark = item.Remark,
                    children = GetChildren(item.Id),
                };
                deptViewList.Add(tree);
            }
            return deptViewList;
        }
        #region old tree 有问题
        public void GetAllChildren(List<Department> deptTopList, List<DepartmentView> resultList)
        {
            foreach (var item in deptTopList)
            {

                //var child = DbSession.DepartmentDal.GetEntities(u => u.DelFlag == 0 && u.ParentId == item.Id).OrderBy(u => u.Sort).ToList();
                DepartmentView tree = new DepartmentView
                {
                    Id = item.Id,
                    DeptName = item.DeptName,
                    DeptCode = item.DeptCode,
                    ParentId = item.ParentId,
                    IsLeaf = item.IsLeaf,
                    IsCheck = item.IsCheck,
                    Sort = item.Sort,
                    children = null,
                };
                resultList.Add(tree);
                if (item.IsLeaf == (short)AllEnum.IsLeafEnum.是)
                {
                    continue;
                }
                List<Department> children = GetTopChildren(item.Id);
                GetAllChildren(children, resultList);
            }
        }
        public List<Department> GetTopChildren(int parentId)
        {
            List<Department> deptAllList = DbSession.DepartmentDal.GetEntities(u => u.DelFlag == 0 && u.ParentId == parentId).OrderBy(u => u.Sort).ToList();

            return deptAllList;
        }
        #endregion
        #region dept tree
        public List<DepartmentView> GetChildren(int parentId)
        {
            List<DepartmentView> deptViewList = new List<DepartmentView>();
            List<Department> deptAllList = DbSession.DepartmentDal.GetEntities(u => u.DelFlag == 0 && u.ParentId == parentId).OrderBy(u => u.Sort).ToList();
            foreach (var item in deptAllList)
            {
                DepartmentView tree = new DepartmentView
                {
                    Id = item.Id,
                    DeptName = item.DeptName,
                    DeptCode = item.DeptCode,
                    ParentId = item.ParentId,
                    IsLeaf = item.IsLeaf,
                    IsCheck = item.IsCheck,
                    Sort = item.Sort,
                    Remark = item.Remark,
                    children = GetChildren(item.Id),
                };
                deptViewList.Add(tree);
            }
            return deptViewList;
        }
        #endregion


    }
}
